package org.gruposp2p.dnie.ui.dialogs;

import dniegp2pclient.DNIeGP2PClientApp;
import java.io.File;
import javax.swing.JFileChooser;
import org.gruposp2p.dnie.utils.FileUtils;
import org.jdesktop.application.Action;

/**
 *
 * @author jj
 */
public class SignedFilesViewerDialog extends javax.swing.JDialog {

    private String xmlSignedDocument;

    /** Creates new form SignedFilesViewerDialog */
    public SignedFilesViewerDialog(
            java.awt.Frame parent, boolean modal, String xmlSignedDocument) {
        super(parent, modal);
        this.xmlSignedDocument = xmlSignedDocument;
        initComponents();
        signedDataEditorPane.setText(xmlSignedDocument);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        signedDataScrollPane = new javax.swing.JScrollPane();
        signedDataEditorPane = new javax.swing.JEditorPane();
        closeButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(DNIeGP2PClientApp.class).getContext().getResourceMap(SignedFilesViewerDialog.class);

        signedDataScrollPane.setBackground(new java.awt.Color(237, 236, 235));
        signedDataScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("signedDataBorder.text")));
        signedDataScrollPane.setForeground(new java.awt.Color(254, 254, 254));
        signedDataScrollPane.setName("signedDataScrollPane"); // NOI18N

        signedDataEditorPane.setBackground(new java.awt.Color(255, 255, 255));
        signedDataEditorPane.setBorder(null);
        signedDataEditorPane.setEditable(false);
        signedDataEditorPane.setName("signedDataEditorPane"); // NOI18N
        signedDataScrollPane.setViewportView(signedDataEditorPane);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(SignedFilesViewerDialog.class, this);
        closeButton.setAction(actionMap.get("close")); // NOI18N
        closeButton.setIcon(resourceMap.getIcon("closeButton.icon")); // NOI18N
        closeButton.setText(resourceMap.getString("closeButtonCaption.text"));
        closeButton.setName("closeButton"); // NOI18N

        saveButton.setIcon(resourceMap.getIcon("saveButton.icon")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButtonCaption.text"));
        saveButton.setName("saveButton"); // NOI18N
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(297, Short.MAX_VALUE)
                .addComponent(saveButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton)
                .addContainerGap())
            .addComponent(signedDataScrollPane)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(signedDataScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(closeButton)
                    .addComponent(saveButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
         JFileChooser chooser = new JFileChooser();
         chooser.showSaveDialog(
                 DNIeGP2PClientApp.getApplication().getView().getFrame());
         File file = chooser.getSelectedFile();
         if (file!= null) {
             FileUtils.save( xmlSignedDocument,
                     file.getAbsolutePath(), "xml");
         }
    }//GEN-LAST:event_saveButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SignedFilesViewerDialog dialog = new SignedFilesViewerDialog(new javax.swing.JFrame(), true, null);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    @Action
    public void close() {
        this.dispose();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JEditorPane signedDataEditorPane;
    private javax.swing.JScrollPane signedDataScrollPane;
    // End of variables declaration//GEN-END:variables

}
